﻿using Ilongshi.Oversea.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ec_System.Models.Web;

namespace Ec_System.Service.Web
{
    public class CustomerService
    {
        /// <summary>
        /// 检测用户名是否存在
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public int GetCustomerCountByLoginID(string loginID)
        {
            var cmd = DataCommandManager.GetDataCommand("GetCustomerCountByLoginID");
            cmd.SetParameterValue("@LoginID", loginID);
            cmd.SetParameterValue("@Tel", null);
            cmd.SetParameterValue("@Email", null);

            return cmd.ExecuteScalar<int>();
        }

        /// <summary>
        /// 检测Email是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public int GetCustomerCountByEmail(string email)
        {
            var cmd = DataCommandManager.GetDataCommand("GetCustomerCountByLoginID");
            cmd.SetParameterValue("@LoginID", null);
            cmd.SetParameterValue("@Tel",null );
            cmd.SetParameterValue("@Email", email);

            return cmd.ExecuteScalar<int>();
        }

        /// <summary>
        /// 检测电话号码是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public int GetCustomerCountByTel(string tel)
        {
            var cmd = DataCommandManager.GetDataCommand("GetCustomerCountByLoginID");
            cmd.SetParameterValue("@LoginID", null);
            cmd.SetParameterValue("@Tel", tel);
            cmd.SetParameterValue("@Email", null);

            return cmd.ExecuteScalar<int>();
        }

        /// <summary>
        /// 讲注册的用户信息入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddCustomer(CustomerData model)
        {
            var cmd = DataCommandManager.GetDataCommand("AddCustomer");
            cmd.SetParameterValue("@LoginID", model.LoginID);
            cmd.SetParameterValue("@Password", model.Password);
            cmd.SetParameterValue("@Email", model.Email);
            cmd.SetParameterValue("@Tel", model.Tel);
            cmd.SetParameterValue("@QQ", model.QQ);

            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CustomerData UserLoginValidate(CustomerData model)
        {
            var cmd = DataCommandManager.GetDataCommand("UserLogin");
            cmd.SetParameterValue("@LoginID", model.LoginID);
            cmd.SetParameterValue("@Email", model.Email);
            cmd.SetParameterValue("@Password", model.Password);

            return cmd.ExecuteEntity<CustomerData>();
        }

        /// <summary>
        /// 根据LoginID获取用户信息
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public CustomerData GetUserInfoByLoginID(string loginID)
        {          
            var cmd = DataCommandManager.GetDataCommand("GetUserInfoByLoginID");
            cmd.SetParameterValue("@LoginID", loginID);

            return cmd.ExecuteEntity<CustomerData>();
        }

        /// <summary>
        /// 根据LoginID获取用户信息及其收货地址
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public CustomerData GetUserInfoAndAddressInfoByLoginID(string loginID)
        {
            CustomerAddressService adr = new CustomerAddressService();
            var cmd = DataCommandManager.GetDataCommand("GetUserInfoByLoginID");
            cmd.SetParameterValue("@LoginID", loginID);
            var model = cmd.ExecuteEntity<CustomerData>();
            //获取收货地址
            model.CustomerAdress = adr.GetCustomerAdrListByLoginID(loginID);

            return model;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUserInfoByLoginID(CustomerData model)
        {
            var cmd = DataCommandManager.GetDataCommand("UpdateUserInfoByLoginID");
            cmd.SetParameterValue("@RealName", model.RealName);
            cmd.SetParameterValue("@Sex", model.Sex);
            cmd.SetParameterValue("@Email", model.Email);
            cmd.SetParameterValue("@QQ", model.QQ);
            cmd.SetParameterValue("@Tel", model.Tel);
            cmd.SetParameterValue("@LoginID", model.LoginID);
            cmd.SetParameterValue("@Birthday", model.Birthday);

            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUserPwdByLoginID(CustomerData model)
        {
            var cmd = DataCommandManager.GetDataCommand("UpdateUserPwdByLoginID");
            cmd.SetParameterValue("@LoginID", model.LoginID);
            cmd.SetParameterValue("@Password", model.Password);

            return cmd.ExecuteNonQuery();
        }
    }
}
